package com.atguigu.lease.web.admin.service.impl;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {
    //service 层
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomAttrValueService roomAttrValueService; //属性
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    @Lazy
    private ApartmentInfoService apartmentInfoService;

    //Mapper层
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Override
    public void customSaveOrUpdate(RoomSubmitVo vo) {
        //只要房间不为空,那么就是更新数据,这里我们不更新,直接 6个配套集合+一个基本信息  全部删除!!
        boolean update = vo.getId() != null;
        boolean roomInfo = super.saveOrUpdate(vo);
        System.out.println("房间信息注入成功");

        //主键id删除的是一条记录,如果是room_id 删除的是多个和这个id关联的记录
        Long id_room = vo.getId();

        if (update) {
            //删除房间的配套信息
            LambdaQueryWrapper<RoomFacility> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RoomFacility::getRoomId, id_room);
            roomFacilityService.remove(wrapper);

            //删除房间的标签信息
            LambdaQueryWrapper<RoomLabel> wrapperLabel = new LambdaQueryWrapper<>();
            wrapperLabel.eq(RoomLabel::getRoomId, id_room);
            roomLabelService.remove(wrapperLabel);

            //删除房间的支付方式信息
            LambdaQueryWrapper<RoomPaymentType> wrapperPaymentTyper = new LambdaQueryWrapper<>();
            wrapperPaymentTyper.eq(RoomPaymentType::getRoomId, id_room);
            roomPaymentTypeService.remove(wrapperPaymentTyper);

            //删除房间的租期信息
            LambdaQueryWrapper<RoomLeaseTerm> wrapperLease = new LambdaQueryWrapper<>();
            wrapperLease.eq(RoomLeaseTerm::getRoomId, id_room);
            roomLeaseTermService.remove(wrapperLease);

            //删除房间的图片信息
            LambdaQueryWrapper<GraphVo> wrapperGraph = new LambdaQueryWrapper<>();
            wrapperGraph.eq(GraphVo::getItemType, ItemType.ROOM);
            wrapperGraph.eq(GraphVo::getItemId, id_room);
            graphInfoService.remove(wrapperGraph);

            //删除房间的属性信息
            LambdaQueryWrapper<RoomAttrValue> wrapperAttr = new LambdaQueryWrapper<>();
            wrapperAttr.eq(RoomAttrValue::getRoomId, id_room);
            roomAttrValueService.remove(wrapperAttr);

        }
        System.out.println("保存操作开始执行");
        //保存___配套信息
        List<Long> arr_facilityId = vo.getFacilityInfoIds();
        //配套信息id不为空,说明前端选择了标签
        if (!CollectionUtils.isEmpty(arr_facilityId)) {
            //这里创建集合的目的是添加 RoomFacility 类型的 因为上面是 long类型 不匹配
            ArrayList<RoomFacility> arr_facility = new ArrayList<>();
            for (Long s : arr_facilityId) {
                RoomFacility roomFacility = RoomFacility.builder()
                        .roomId(id_room)
                        .facilityId(s)
                        .build();
                arr_facility.add(roomFacility);
            }
            roomFacilityService.saveBatch(arr_facility);
            System.out.println("配套信息保存成功");
        }

        //保存___标签信息
        List<Long> arr_LabelId = vo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(arr_LabelId)) {
            ArrayList<RoomLabel> arr_label = new ArrayList<>();
            for (Long s : arr_LabelId) {
                RoomLabel roomLabel = RoomLabel.builder()
                        .labelId(s)
                        .roomId(id_room)
                        .build();
                arr_label.add(roomLabel);
            }
            roomLabelService.saveBatch(arr_label);
            System.out.println("标签信息保存成功");
        }

        //保存___图片信息
        List<com.atguigu.lease.web.admin.vo.graph.GraphVo> arr_GraphVo = vo.getGraphVoList();
        if (!CollectionUtils.isEmpty(arr_GraphVo)) {
            ArrayList<GraphVo> arr_Graph = new ArrayList<>();
            for (com.atguigu.lease.web.admin.vo.graph.GraphVo s : arr_GraphVo) {
                GraphVo graphInfo = new GraphVo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(id_room);
                graphInfo.setName(s.getName());
                graphInfo.setUrl(s.getUrl());
                arr_Graph.add(graphInfo);
            }
            graphInfoService.saveBatch(arr_Graph);
            System.out.println("图片信息保存成功");
        }

        //保存___属性信息
        List<Long> arr_attrId = vo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(arr_attrId)) {
            ArrayList<RoomAttrValue> arr_attrValue = new ArrayList<>();
            for (Long s : arr_attrId) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                        .roomId(id_room)
                        .attrValueId(s)
                        .build();
                arr_attrValue.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(arr_attrValue);
        }

        //保存___支付方式信息
        List<Long> arr_payTypeId = vo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(arr_payTypeId)) {
            ArrayList<RoomPaymentType> arr_payType = new ArrayList<>();
            for (Long s : arr_payTypeId) {
                RoomPaymentType paymentType = RoomPaymentType.builder()
                        .roomId(id_room)
                        .paymentTypeId(s)
                        .build();
                arr_payType.add(paymentType);
            }
            roomPaymentTypeService.saveBatch(arr_payType);
        }

        //保存___租期信息
        List<Long> arr_LeaseId = vo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(arr_LeaseId)) {
            ArrayList<RoomLeaseTerm> arr_lease = new ArrayList<>();
            for (Long s : arr_LeaseId) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .roomId(id_room)
                        .leaseTermId(s)
                        .build();
                arr_lease.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(arr_lease);

        }
    }

    @Override
    public void customQueryPageData(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customQueryPageData(page, queryVo);
    }

    @Override
    //未完成
    public RoomDetailVo customQueryById(Long id) {

        RoomDetailVo vo = new RoomDetailVo();
        //查询房间的详情
        RoomInfo roomInfo = getById(id);
        // 获取公寓信息


        // 获取图片列表
        //List<GraphVo> graphInfoList=  graphInfoMapper.QueryGraph(id,ItemType.ROOM);

        // 获取标签列表
        List<LabelInfo> labelInfoList = roomLabelMapper.QueryLabel(id);
        // 获取配套信息列表
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.QueryFacility(id);
        // 获取属性信息列表
        List<AttrValueVo> attrValueList =  roomAttrValueMapper.QueryAttrValue(id);
        // 获取支付方式列表
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.QueryPaymentType(id);
        // 获取租期列表
        List<LeaseTerm> LeaseTerm =  roomLeaseTermMapper.QueryLeaseTerm(id);
        //保存数据
        vo.setLabelInfoList(labelInfoList);
        vo.setFacilityInfoList(facilityInfoList);
        vo.setPaymentTypeList(paymentTypeList);
        vo.setLeaseTermList(LeaseTerm);



        return vo;
    }

    @Override
    public void customRemoveById(Long id) {
        LambdaQueryWrapper<RoomInfo> wrapper_room = new LambdaQueryWrapper<>();
        wrapper_room.eq(RoomInfo::getApartmentId, id);
        long count = super.count(wrapper_room);
        if (count>0){
            //只要抛出异常,程序就不会往下运行了
            throw new RuntimeException("该公寓下有房间存在,请先删除该公寓下的房间");
        }
        // 删除公寓信息
        LambdaQueryWrapper<ApartmentInfo> wrapper_apart = new LambdaQueryWrapper<>();
        wrapper_apart.eq(ApartmentInfo::getId, id);
        apartmentInfoService.remove(wrapper_apart);
        System.out.println("公寓信息删除成功");

        // 删除图片信息
        QueryWrapper<com.atguigu.lease.model.entity.GraphVo> wrapper_graph = new QueryWrapper<>();
        wrapper_graph.eq("item_type",id);
        System.out.println("图片信息删除成功");

        // 删除标签信息
        LambdaQueryWrapper<RoomLabel> wrapper_label = new LambdaQueryWrapper<>();
        wrapper_label.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(wrapper_label);
        System.out.println("标签信息删除成功");

        // 删除配套信息
        LambdaQueryWrapper<RoomFacility> wrapper_facility = new LambdaQueryWrapper<>();
        wrapper_facility.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(wrapper_facility);
        System.out.println("配套信息删除成功");

        // 删除属性信息
        LambdaQueryWrapper<RoomAttrValue> wrapper_attrValue = new LambdaQueryWrapper<>();
        wrapper_attrValue.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(wrapper_attrValue);
        System.out.println("属性信息删除成功");

        // 删除支付方式信息
        LambdaQueryWrapper<RoomPaymentType> wrapper_paymentType = new LambdaQueryWrapper<>();
        wrapper_paymentType.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(wrapper_paymentType);
        System.out.println("支付方式信息删除成功");

    }
}




